package com.oa.controller;

import com.oa.pojo.Dept;
import com.oa.pojo.Result;
import com.oa.service.DeptService;
import com.oa.service.impl.DeptServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "部门：在线接口")
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @CrossOrigin
    @ApiOperation(value="根据部门条件查询(id,parentId,grade)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/selectAll")
    public Result selectDebt(Integer id,Integer parentId,Integer grade){
        System.out.println("执行查询部门条件接口");
        Dept dept = new Dept();
        dept.setId(id);
        dept.setGrade(grade);

        List<Dept> deptList = deptService.selectDept(dept);
        return Result.success(deptList);
    }

    @CrossOrigin
    @ApiOperation(value="根据部门条件删除(id)",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/deleteDept")
    public Result deleteDept(Integer id){
        System.out.println("执行部门删除接口");
        int flag=deptService.deleteDept(id);
        if (flag==1){
            return Result.success();
        }else {
            return Result.error("没有此id的部门");
        }
    }


    @CrossOrigin
    @ApiOperation(value="部门信息更改",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/updateDept")
    public Result updateDept(@RequestBody Dept dept){
        System.out.println("执行部门信息修改接口");

        int flag=deptService.updateDept(dept);
        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加心部门失败，有重复属性");
        }
    }

    @CrossOrigin
    @ApiOperation(value="添加部门",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/insertDept")
    public Result insertDept(@RequestBody Dept dept){
        System.out.println("执行添加部门接口");

        int flag=deptService.insertDept(dept);

        if (flag==1) {
            return Result.success();
        }else {
            return Result.error("添加心部门失败，有重复属性");
        }
    }

}
